/*******************************************
Module: zt_str.h
E-Mail: 24zhutian@gmail.com
Notices: Copyright (c) 2007-2011 ls
*******************************************/
#ifndef zt_str_h
#define zt_str_h

#ifdef __cplusplus
extern "C" {
#endif

#define CP_GB2312				936

#define ZT_KB					1024
#define ZT_MB					ZT_KB*ZT_KB
#define ZT_GB					ZT_MB*ZT_MB

typedef struct zt_str_s{
	u_char *str;
	size_t use;
	size_t siz;
}zt_str_t;

#define ZT_MEM_MALLOC(type,siz)	(type *)malloc((siz)*sizeof(type))
#define ZT_MEMEND(p,siz)		(*((p)+(siz))='\0')
	
#define ZT_CHAR_LOWER(c)		(u_char)((c>='A' && c<='Z')?(c | 0x20) : c)
#define ZT_CHAR_UPPER(c)		(u_char)((c>='a' && c<='z')?(c & ~0x20): c)
#define ZT_MEMCPY(d,s,n)		(u_char *)memcpy(d,s,n)
#define ZT_MEMCMP(d,s,n)		memcmp(d,s,n)
#define ZT_MEMCHR(s,c,n)		(u_char *)memchr(s,c,n)
//#define zt_str_not_null(s)		((s != NULL) ? (( *s != 0) ? 1 : 0) : 0)

#define ZT_STR_INIT(s,p,u,n)	do{(s)->str=p;(s)->use=u;(s)->siz=n;}while(0)
#define ZT_STR_SET(s,p,siz)		do{(s)->str=p;(s)->use=siz;}while(0)
#define ZT_STR_RESET(s)			do{(s)->use=0;*((s)->str)='\0';}while(0)
#define ZT_STR_END(s)			*((s)->str+(s)->use)='\0'
#define ZT_STR_MEMCPY(d,s)		ZT_MEMCPY((d)->str,(s)->str,(s)->use);(d)->use=(s)->use;
#define ZT_STR_MEMCAT(d,s)		ZT_MEMCPY((d)->str+(d)->use,(s)->str,(s)->use);(d)->use+=(s)->use;
#define ZT_STR_MEMCHR(s,c)		(u_char *)ZT_MEMCMP((s)->str,c,(s)->use)

#define ZT_STR_ISEMPTY(s)		((s)->use==0)

#define ZT_STR_C_STR(s)			(s)->str
#define ZT_STR_LENGTH(s)		(s)->use

#define ZT_BASE64_ESCAPE_SIZ(siz)			(((siz+2)/3)*4)
#define ZT_BASE64_UNESCAPE_SIZ(siz)			(((siz+3)/4)*3)
/* GB2312 and UTF-8 convert */
#ifdef _MSC_VER
#define	ZT_CS_GB2312_UTF8(dst,src,siz,wbuf)	zt_cs_charset(&dst,CP_GB2312,CP_UTF8,src,siz,wbuf)
#define	ZT_CS_UTF8_GB2312(dst,src,siz,wbuf)	zt_cs_charset(&dst,CP_UTF8,CP_GB2312,src,siz,wbuf)
#endif

ZT_API size_t		zt_strlen(const u_char *str);

ZT_API u_char*		zt_tolower(u_char *str);
ZT_API u_char*		zt_toupper(u_char *str);
	
ZT_API u_char*		zt_strcpy(u_char *to,const u_char *from);
ZT_API u_char*		zt_strncpy(u_char *to,const u_char *from,size_t n);
	
ZT_API u_char*		zt_strcat(u_char *str,const u_char *append);
ZT_API u_char*		zt_strncat(u_char *dst,const u_char *src,size_t siz);
	
ZT_API int			zt_strcmp(const u_char *s1, const u_char *s2);
ZT_API int			zt_strncmp(const u_char *s1, const u_char *s2, size_t n);
	
ZT_API int			zt_strcasecmp(u_char *s1,u_char *s2);
ZT_API int			zt_strncasecmp(u_char *s1,u_char *s2,size_t n);
	
ZT_API u_char*		zt_strchr(const u_char *s,u_char c);
ZT_API u_char*		zt_strstr(const u_char* src,u_char *dst);
	
ZT_API size_t		zt_strchrcpy(u_char *dst,const u_char *s,u_char c);
	
ZT_API u_char*		zt_strdup(u_char *str,size_t siz);
	
/* uri/base64 escape/unescape */
ZT_API size_t		zt_uri_escape_siz(const u_char *str);
ZT_API size_t		zt_uri_escape(u_char *dst,const u_char *src);
	
ZT_API size_t		zt_uri_unescape_siz(const u_char *str);
ZT_API size_t		zt_uri_unescape(u_char *dst,const u_char *src);

ZT_API size_t		zt_hex_escape(u_char *dst,const u_char *src);
ZT_API size_t		zt_hex_unescape(u_char *dst,const u_char *src);
	
ZT_API size_t		zt_base64_escape(u_char *dst,const u_char *str,size_t siz);
ZT_API size_t		zt_base64_unescape(u_char *dst,const u_char *str,size_t siz);
	
ZT_API zt_int_t		zt_atoi(u_char *str, size_t n);

ZT_API int			zt_ungzip(zt_str_t *dst,zt_str_t *src);
ZT_API size_t		zt_cs_charset(u_char **dst,unsigned int cs,unsigned int tcs,u_char *src,size_t n,u_char *wbuf);

ZT_API zt_str_t*	zt_str_alloc(size_t siz);
ZT_API zt_int_t		zt_str_free(zt_str_t *str);

ZT_API size_t		zt_str_strcpy(zt_str_t *dst,u_char *src);
ZT_API size_t		zt_str_strcat(zt_str_t *dst,u_char *src);

/*
ZT_API size_t		zt_str_memcat(zt_str_t *dst,u_char *src,size_t siz);
ZT_API size_t		zt_str_append(zt_str_t *dst,zt_str_t *src);

ZT_API size_t		zt_str_ltrim(zt_str_t *s);
ZT_API size_t		zt_str_rtrim(zt_str_t *s);
ZT_API size_t		zt_str_trim(zt_str_t *s);

ZT_API int			zt_str_find(zt_str_t *str,u_char *s);
ZT_API int			zt_str_substr(u_char *p,zt_str_t *str,size_t pos,int n);
ZT_API size_t		zt_str_chr_replace(zt_str_t *str,u_char oldch,u_char newch);
ZT_API size_t		zt_str_replace(zt_str_t *str,u_char *olds,u_char *news);
*/

#ifdef __cplusplus
}
#endif /* C++ */
#endif /* zt_str_h */